#include "TTab.h"
#include "TUI.h"
#include "../utils/Utils.h"

TTab* TTab::create(xml_node<> * pItem){
    
    TTab*  ret=TTab::create();
    ret->initWidthConf(pItem);
    return ret;
}

TTab::~TTab(){
    for(uint32 i=0;i<gPanels.size();i++){
        gPanels[i]->release();
    }
    
}


TTab::TTab(){
    m_pListener=NULL;
    m_pfnSelector=NULL;
    m_pCurPanel=NULL;
    m_pCurBtn=NULL;
}

void TTab::initWidthConf(xml_node<> * pItem){
   
    if(!pItem) return;
    CCSize size=readContainSize(pItem,getParent());
    if(size.width ==-1) size.width=getContentSize().width;
    if(size.height==-1) size.height=getContentSize().height;
    setContentSize(size);
    
    parseConf(pItem->first_node());
    
    
    onCreateComplete();
}


void TTab::onCreateComplete(){
    
    CCArray * children = getChildren();
    TPanelItem * node = 0;
    int count = children->count();
    CCPoint pos;
    for (int i = 0; i < count; ++i)
    {
        node = (TPanelItem*)children->objectAtIndex(i);
        if(node->tType==T_BUTTON_NAME){
            gBtns.push_back((TButton*)node);
            ((TButton*)node)->setTarget(this, menu_selector(TTab::onBtnClick));
        }else{
            node->retain();
            gPanels.push_back((TPanel*)node);
        }

    }
    
   
    if(gBtns.size()!=0){
        selectBtn(gBtns[0]);
    }
}

TPanel* TTab::getCurPanel(){
    
    return m_pCurPanel;
    
}
TButton* TTab::getCurTButton(){
    
    return m_pCurBtn;
}

void TTab::onBtnClick(CCObject* obj){
    selectBtn((TButton*)obj);
}
void TTab::clear(){ 
    for(unsigned int i=0;i<gPanels.size();i++){
        
        gPanels[i]->setOpacity(255);
        if(gPanels[i]->getParent()){
            gPanels[i]->getParent()->removeChild(gPanels[i], true);
        }
    }
    
    for(unsigned int i=0;i<gBtns.size();i++){
         
        gBtns[i]->setEnable(true);
    }
}


TButton* TTab::getBtnByTag(int tag){ 
    for(uint32 i=0;i<gBtns.size();i++){
        
        vector<string>ret;
        splitStr(ret, gBtns[i]->linkPanel, ",");
        for(unsigned int j=0;j<ret.size();j++){
            TPanelItem* item=getUI(ret[j].c_str());
            if(tag==item->getTag()){
                return (gBtns[i]);
                
            }
            
        } 
        
    }
    return NULL;
}
void TTab::changeTab(int tag){
    
    TButton* btn=getBtnByTag(tag);
    
    if(!btn){
        return;
    }
    selectBtn(btn);
}
void TTab::changeIdx(int idx){
    TButton* btn=gBtns[idx];
    
    if(!btn){
        return;
    }
    selectBtn(btn);
}

void TTab::selectBtn(TButton* btn){
    if(btn->linkPanel.size()==0){
        return;
    }
    clear();
    btn->setEnable(false);
    m_pCurBtn=btn;
    
    vector<string>ret;
    splitStr(ret, btn->linkPanel, ","); 
    for(unsigned int i=0;i<ret.size();i++){ 
 
        TPanelItem* item=getUI(ret[i].c_str());
        if(item){
            m_pCurPanel=(TPanel*)item;
        }
        if(item && item->getParent()==NULL){
            addChild(item,-1);
        }
        
    }
    
    
    if(m_pListener&& m_pfnSelector)
        (m_pListener->*m_pfnSelector)(btn);
}

void TTab::setTabChangeTarget(CCObject*  listener,SEL_MenuHandler    selector){
    
     m_pListener=listener;
     m_pfnSelector=selector;
}



